clear all 

* ---------------------------------------------------------------------------- *
* Step 1: Import data and adjust variables for early repayment ratio
* ---------------------------------------------------------------------------- *

import delimited "$input\duration_ratio.csv"  // Import CSV file containing repayment ratios
rename (v1) (index)  // Rename column V1 to 'index' for clarity

* Convert ratio to percentage scale
replace ratio = ratio * 100  

* ---------------------------------------------------------------------------- *
* Step 2: Histogram of early repayment ratios - Figure H1
* ---------------------------------------------------------------------------- *

histogram ratio, percent graphregion(color(white)) ytitle("% of FinTech loans") /*
*/  yscale(titlegap(2)) xscale(titlegap(2))  xlabel(, grid gstyle(dot) glcolor(gs10%40)) /*
*/ ylabel(, grid gstyle(dot) glcolor(gs10%40)) xtitle("Time of repayment/Maturity (%)") /*
*/ fcolor(gs10%40) lcolor(gs0) lwidth(0.2)  // Customizing graph appearance

* Export histogram as PDF and Stata graph file
graph export "$figures\early_repayers.pdf", as(pdf) replace
graph save Graph "$figures\early_repayers.gph", replace

****************************************
* Step 3: Import data and adjust for observed duration in months
****************************************

clear all  // Clear memory before loading new dataset

import delimited "$input\duration_observed_in_months.csv"  // Import dataset
rename (v1) (index)  // Rename column V1 to 'index' for clarity

* ---------------------------------------------------------------------------- *
* Step 4: Histogram of observed duration before repayment - Figure H2
* ---------------------------------------------------------------------------- *

histogram observed, percent graphregion(color(white)) ytitle("% of FinTech loans") /*
*/  yscale(titlegap(2)) xscale(titlegap(2))  xlabel(0(12)60, grid gstyle(dot) glcolor(gs10%40)) /*
*/ ylabel(, grid gstyle(dot) glcolor(gs10%40)) xtitle("Months before repayment") /*
*/ fcolor(gs10%40) lcolor(gs0) lwidth(0.2)  // Customizing graph appearance

* Export histogram as PDF and Stata graph file
graph export "$figures\early_repayers_in_months.pdf", as(pdf) replace
graph save Graph "$figures\early_repayers_in_months.gph", replace
